package org.enwey.icarenew.Activity.News;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestListResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Adapter.CommentAdapter;
import org.enwey.icarenew.Model.Comment;
import org.enwey.icarenew.Model.Topic;
import org.enwey.icarenew.R;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by John on 3/17/2015.
 */
public class CommentActivity extends BaseActivity {
    private PullToRefreshListView mListView;
    private CommentAdapter mAdapter;
    private EditText mComment;
    private TextView mShare;
    private String mTopicId;
    private int currentPage;
    private List<Comment> mCommentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        setUp();
        findViews();
        CallAPIGetCommentList();
        registerListener();
    }

    private void setUp() {
        setTitle("评论");
        showLeftBtn();
        mTopicId = getIntent().getStringExtra("newId");
        mAdapter = new CommentAdapter(this);
        mCommentList = new ArrayList<>();
        mAdapter.setMenuStyleList(mCommentList);
    }

    private void findViews() {
        mListView = (PullToRefreshListView) this.findViewById(R.id.activity_comment_list);
        mComment = (EditText) this.findViewById(R.id.activity_comment_comment);
        mShare = (TextView) this.findViewById(R.id.activity_comment_share);

        mListView.setAdapter(mAdapter);
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.activity_comment_share) {
                    ToastUtil.show(CommentActivity.this, "click");
                }
            }
        };
        mShare.setOnClickListener(register);
    }

    private void CallAPIGetCommentList() {
        showLoading();
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("page", currentPage + "");

        APIManager.getInstance(this).getCommentList(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                hideLoading();
                mListView.onRefreshComplete();
                ToastUtil.show(CommentActivity.this, "连接失败");
            }
        }, new Response.Listener<RequestListResult<Comment>>() {
            @Override
            public void onResponse(RequestListResult<Comment> response) {
                hideLoading();
                if (response.data != null && response.data.size() > 0) {
                    if (currentPage == 0) {
                        mCommentList = response.data;
                        mAdapter.setMenuStyleList(mCommentList);
                    } else if (currentPage > 0 && mCommentList != null) {
                        mCommentList.addAll(response.data);
                    }
                    mAdapter.notifyDataSetChanged();
                    mListView.onRefreshComplete();
                }

                if (mAdapter.getCount() == 0) {
                    showNoData();
                } else if (response.code == 201 || response.data.size() < 10) {
                    mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    ToastUtil.show(CommentActivity.this, "最后一页");
                } else {
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
                mListView.onRefreshComplete();
            }
        });

    }

}
